import Api from "../../api"

const state = {
    userList:[]
}

const mutations = {
    setUserList(state,userList){
        state.userList = userList; 
    }
}

const actions = {
    async QueryAll({commit},status){
        if(status === null){
            let {data} = await Api.User.QueryAll()
            console.log(data.data);
            commit('setUserList',data.data);
        }else{
            let {data} = await Api.User.Query(status);
            commit('setUserList',data.data)
        }
        
    },
    UpdateUser({commit},user){
        return new Promise((resolve,reject)=>{
            setTimeout(async()=>{
                let {data}=await Api.User.Update(user)
                .catch(error=>reject(error))
                resolve(data.data)
            },500)
        })
    }
}

const getters = {
    QueryAll(state){ 
        return state.userList.map(user=>{
            user.status = user.status === 0 ? '正常' : '冻结';
            user.flag = user.flag === 1 ? '未回收' : '已回收';
            let keys = Object.keys(user)
            keys.map(item=>{
                user[item] = user[item] === null ? '未设置' : user[item];
            })
            return user;
        })
    }
}

export default {
    state, mutations, actions, getters
}